package com.aerozhonghuan.oknet2;

import android.content.Context;
import android.text.TextUtils;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.WeakHashMap;

/**
 * http 请求 构造器
 * 目前不支持 文件上传的同步放过
 * 不支持 build 方式
 * Created by zhangyunfei on 15/12/15.
 */
public class RequestBuilder {
    private WeakReference<Context> activityWeakReference;
    private String url;
    private HashMap<String, String> paras;
    private HashMap<String, String> headers;
    private CommonCallback callback;
    private ProgressIndicator mProgressObserver;
    private WeakHashMap<String, File> fileParas;
    private String bodyString;
    private boolean useGetMethod = false;
    private boolean useSynchronous = false;//是否同步执行

    private RequestBuilder() {
        paras = new HashMap<>();
        headers = new HashMap<>();
        fileParas = new WeakHashMap<>();
    }

    public static RequestBuilder with(Context activity) {
        RequestBuilder myRequestBuilder = new RequestBuilder();
        myRequestBuilder.setContext(activity);
        return myRequestBuilder;
    }

    public void setContext(Context context) {
        this.activityWeakReference = new WeakReference<>(context);
    }

    public RequestBuilder URL(String url) {
        this.url = url;
        return this;
    }

    public RequestBuilder para(String key, String value) {
        paras.put(key, value);
        return this;
    }

    public RequestBuilder para(String key, int value) {
        paras.put(key, value + "");
        return this;
    }

    public RequestBuilder addHeader(String key, int value) {
        headers.put(key, value + "");
        return this;
    }
    public RequestBuilder addHeader(String key, String value) {
        headers.put(key, value + "");
        return this;
    }

    public RequestBuilder body(String bodyString) {
        this.bodyString = bodyString;
        return this;
    }

    public RequestBuilder useSynchronous() {
        useSynchronous = true;
        return this;
    }

    public RequestBuilder useGetMethod() {
        this.useGetMethod = true;
        return this;
    }

    public RequestBuilder file(String key, File value) {
        fileParas.put(key, value);
        return this;
    }

    public RequestBuilder onSuccess(CommonCallback myJsonResponseHandler3) {
        this.callback = myJsonResponseHandler3;
        return this;
    }

    public RequestBuilder progress(ProgressIndicator isShowProgress) {
        this.mProgressObserver = isShowProgress;
        return this;
    }

    public CommonRequest build() {
        CommonRequest request;
        if (fileParas.size() > 0) {
            request = new FileUploadRequest(url, paras, fileParas, callback);
            request.useGetMethod(false);
        } else {
            if (!TextUtils.isEmpty(bodyString)) {
                request = new BodyStringRequest(url, bodyString, callback);
                request.useGetMethod(false);
            } else {
                request = new CommonRequest(url, paras, callback);
                request.useGetMethod(this.useGetMethod);
            }
        }
        //构建headers
        request.getHeaders().putAll(headers);
        request.setContext(activityWeakReference.get());
        request.setProgressIndicator(mProgressObserver);
        return request;
    }

    /**
     * 执行请求
     *
     * @return CommonRequest
     */
    public OkNetCall excute() {
        try {
            CommonRequest request = this.build();
            if (request instanceof FileUploadRequest) {
                return RequestManager.excute((FileUploadRequest) request, useSynchronous);
            } else if (request instanceof BodyStringRequest) {
                return RequestManager.excute((BodyStringRequest) request, useSynchronous);
            } else {
                return RequestManager.excute(request, useSynchronous);
            }
        } finally {
            clear();
        }
    }

    private void clear() {
        if (activityWeakReference != null && activityWeakReference.get() != null)
            activityWeakReference.clear();
        url = null;
        if (paras != null)
            paras.clear();
        paras = null;
        callback = null;
        mProgressObserver = null;
    }


}
